//
//  XSJEmoticonKeyBoard.swift
//  weiBo项目准备
//
//  Created by 鲜世杰 on 16/5/20.
//  Copyright © 2016年 鲜世杰. All rights reserved.
//

import UIKit

private let XSJEmoticonCollectionViewCellIdentifier = "XSJEmoticonCollectionViewCellIdentifier"


class XSJEmoticonKeyBoard: UIView {
    
    private lazy var toolBar : XSJEmoticonToolBar = {
        let view = XSJEmoticonToolBar(frame: CGRectZero)
        return view
    }()
    
    
    private lazy var emoticonCollectionView: UICollectionView = {
        
        let flowLayout = UICollectionViewFlowLayout()
        
        
        flowLayout.scrollDirection = .Horizontal
        
        let view = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
        view.backgroundColor = self.backgroundColor

        view.pagingEnabled = true
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        view.bounces = false

        
        view.registerClass(XSJEmoticonCollectionViewCell.self, forCellWithReuseIdentifier: XSJEmoticonCollectionViewCellIdentifier)
        
        view.dataSource = self
        
        
        view.delegate = self

        return view
        
    }()
    
    
    private lazy var pageControl: UIPageControl = {
        
        let ctr = UIPageControl()
        
        ctr.currentPageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_selected")!)
        
        ctr.pageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
        
        
        return ctr
        
    }()

    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
//        backgroundColor = RandomColor()
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)

        let indexPath : NSIndexPath = NSIndexPath(forItem: 0, inSection: 0)
        
        setPageControlData(indexPath)
        
        addSubview(toolBar)
        
        addSubview(emoticonCollectionView)
        
        addSubview(pageControl)
        
        emoticonCollectionView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)
        }

        pageControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)
            make.height.equalTo(10)
        }
        
        toolBar.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.height.equalTo(40)
        }
        
        toolBar.didSelectedEmoticonButtonClosure = {[weak self] (type: XSJEmoticonToolBarButtonType) in
            let indexPath: NSIndexPath

            switch type {
            case .Normal:
//                print("Normal")
                indexPath = NSIndexPath(forItem: 0, inSection: 0)
            case .Emoji:
//                print("Emoji")
                indexPath = NSIndexPath(forItem: 0, inSection: 1)
            case .Lxh:
//                print("Lxh")
                indexPath = NSIndexPath(forItem: 0, inSection: 2)

            }
            
            
            self?.emoticonCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            
            self?.setPageControlData(indexPath)


        }

    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let flowLayout = emoticonCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
       
        flowLayout.itemSize = emoticonCollectionView.frame.size
       
        flowLayout.minimumInteritemSpacing = 0
        
        flowLayout.minimumLineSpacing = 0
        
        
    }
    
    private func setPageControlData(indexPath : NSIndexPath) {
    
        pageControl.numberOfPages = XSJEmoticonTools.sharedTools.allEmoticons[indexPath.section].count
        
        pageControl.currentPage = indexPath.item
        
    }
    
}
extension XSJEmoticonKeyBoard: UICollectionViewDataSource,UICollectionViewDelegate {
    
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return XSJEmoticonTools.sharedTools.allEmoticons.count
    }
   
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return XSJEmoticonTools.sharedTools.allEmoticons[section].count
    }
    
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
//        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(XSJEmoticonCollectionViewCellIdentifier, forIndexPath: indexPath)
//        cell.backgroundColor = RandomColor()

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(XSJEmoticonCollectionViewCellIdentifier, forIndexPath: indexPath) as! XSJEmoticonCollectionViewCell
        cell.indexpath = indexPath

        cell.emotions = XSJEmoticonTools.sharedTools.allEmoticons[indexPath.section][indexPath.item]
        
        return cell
        
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {

    
        
        let contentOffSetX = scrollView.contentOffset.x
        let cells = emoticonCollectionView.visibleCells().sort { (firstCell, secondCell) -> Bool in
            return firstCell.x < secondCell.x
        }
        
        if cells.count == 2 {
        
            let firstCell = cells.first!
            let secondCell = cells.last!
            let firstCellContentOffSetX  = abs(firstCell.x - contentOffSetX)
            let secondCellContentOffSetX = abs(secondCell.x - contentOffSetX)
            
            let indexPath: NSIndexPath

            if firstCellContentOffSetX < secondCellContentOffSetX {
                indexPath = emoticonCollectionView.indexPathForCell(firstCell)!

            } else {
                indexPath = emoticonCollectionView.indexPathForCell(secondCell)!

            }

            let sction = indexPath.section
            toolBar.selectEmotionButtonWithSection(sction)
            setPageControlData(indexPath)
        }
        
    }
    
}


